<?php
namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "articles".
 *
 * @property integer $id
 * @property integer $game_id
 * @property string $title
 * @property string $content
 * @property string $logo_path
 * @property string $kind
 * @property string $author
 * @property integer $sort
 * @property integer $created_at
 * @property integer $updated_at
 */
class Article extends \yii\db\ActiveRecord
{

    // 类别：新闻
    const KIND_NEWS = 1;
    // 类别：活动
    const KIND_ACTIVITY = 2;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'articles';
    }

    /*
     * 产生创建日期和更新日期时间戳
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'game_id',
                    'sort',
                    'kind',
                    'created_at',
                    'updated_at'
                ],
                'integer'
            ],
            [
                [
                    'title',
                    'kind'
                ],
                'required'
            ],
            [
                [
                    'content'
                ],
                'string'
            ],
            [
                [
                    'title',
                    'author',
                    'logo_path'
                ],
                'string',
                'max' => 255
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'game_id' => '游戏ID',
            'title' => '标题',
            'content' => '内容',
            'logo_path' => 'Logo',
            'kind' => '类别',
            'author' => '作者',
            'sort' => '排序值',
            'created_at' => '创建日期',
            'create_time' => '创建日期',
            'updated_at' => '更新日期'
        ];
    }

    /**
     * 所属游戏
     */
    public function getGame()
    {
        return $this->hasOne(Game::className(), [
            'game_id' => 'game_id'
        ]);
    }

/**
 * 获取创建时间，注：仅支持数据模型，当asArray就无法找到这个属性了，本想用在ajax-list中，js不用写一个格式化时间戳的函数
 */
    // public function getCreate_time()
    // {
    // return date('Y-m-d', $this->attributes['created_at']);
    // }
}
